<template>
  <fieldset class="sk">
    <legend>收款信息</legend>
    <div style="display: flex;margin-bottom: 20px;">
      <div>
        <div>
          <j-space>
            <div style="width: 50px;">账户<span style="color: red;padding-left: 5px;">*</span></div>
          </j-space>
          <j-space><JAuto size="small" :width="120" cat="accountList" v-model:id="accId" v-model="accName"   /></j-space>
        </div>
        <div style="margin-top: 5px;">
          <j-space><div style="width: 50px;">金额<span style="color: red;padding-left: 5px;">*</span></div></j-space>
          <j-space><JInput size="small" :width="120" type="number" v-model="accAmount" clearable /></j-space>
        </div>
        <div style="margin-top: 5px;">
          <j-space><div style="width: 50px;">账户1</div></j-space>
          <j-space><JAuto size="small" :width="120" cat="accountList" v-model:id="accId1" v-model="accName1"   /></j-space>
        </div>
        <div style="margin-top: 5px;">
          <j-space><div style="width: 50px;">金额1</div></j-space>
          <j-space><JInput size="small" :width="120" type="number" v-model="accAmount1" clearable /></j-space>
        </div>
      </div>
      <div style="margin-left: 10px;border-left: 1px solid rgba(0,0,0,0.2);padding-left: 20px;">
        <div>
          <j-space>
            <div><div style="width: 50px;">日期</div></div>
            <div><JPicker :width="120" v-model="billDate" /></div>
          </j-space>
        </div>
        <div style="margin-top: 5px;">
          <j-space>
            <div><div style="width: 50px;">经办人</div></div>
            <div><JAuto size="small" :width="120" cat="adminList" v-model:id="doId" v-model="doName"  /></div>
          </j-space>
        </div>
        <div style="margin-top: 5px;">
          <j-space>
            <div><div style="width: 50px;">备注</div></div>
            <div> <JInput :width="120" size="small" v-model="remark" clearable /></div>
          </j-space>
        </div>
      </div>
    </div>
    <tiny-divider></tiny-divider>
    <div style=" display: flex;margin-top: 30px;">
      <div style="width: 200px;">
        <span style="color: #515a6e; font-weight: bold">应收：</span><span style="color: #f56c6c; font-weight: bold">{{ yinsAmount }}</span>
      </div>
      <div>
        <JButton type="info" size="small" @click="verify">审核 </JButton>
      </div>
    </div>
  </fieldset>
</template>
<script>
import tool from "@/utils/tool";

export default {
  name: "account",
  data () {
    return {
      billDate: tool.getDay(),
      accName: '',
      accName1: '',
      accAmount: '',
      accAmount1: '',
      yinsAmount: '',
      remark: '',
      accId: null,
      accId1: null,
      doId: null,
      doName: null,
    }
  },
  emits: ['clearData'],
  methods: {
    setAmount (amount) {
      this.yinsAmount = tool.xe.toFixed(amount, 2)
    },
    setDate (date) {
      this.billDate = date
    },
    setDo (date) {
      this.doId = date?date.id:null
      this.doName = date?date.name:null
    },
    verify() {
      if(!this.accId) return  tool.err('请选择账户');
      if(!this.accAmount) return  tool.err('请输入金额');
      if(!this.doId) return  tool.err('请选择经办人');
      if (this.accId1 && !this.accAmount1) return tool.err('请输入账户1金额');
      if (!this.accId1 && this.accAmount1) return tool.err('请选择账户1');
      let data = this.$parent.getAllData()
      let tuList = data.back.list.filter(row => {
        return tool.xe.toNumber(row.qty) > 0
      })
      if (tuList.length > 0) {
        for (let i = 0; i < tuList.length; i++) {
          let amount = tuList[i].amount ? tool.xe.toNumber(tuList[i].amount) : 0;
          tuList[i].calNum = tuList[i].unit === tuList[i].unit1 ? tuList[i].spec : 1
          if (amount === 0) {
            return tool.err('退货金额不能为 0');
          }
        }
      }
      if (tool.xe.toNumber(this.yinsAmount) !== tool.xe.add(this.accAmount, this.accAmount1)) {
        return tool.err('账户合计金额不等于应收金额');
      }
      let receipts = [];
      data.receiptInfos.map(row => {
        receipts.push({
          billId: row.id,
          buName: row.buName,
          billNo: row.billNo,
          transType: row.transType,
          transTypeName: row.transTypeName,
          totalAmount: row.totalAmount,
          billState: row.billState,
          billDate: row.billDate,
          leftAmount: tool.xe.toNumber(row.leftAmount),
          nowCheck: tool.xe.toNumber(row.nowCheck),
          yhAmount: tool.xe.toNumber(row.yhAmount),
        })
      })
      let model = {
        receipts: receipts,
        back: data.back,
        billDate: this.billDate,
        accId: this.accId,
        accName: this.accName,
        accAmount: this.accAmount,
        accId1: this.accId1,
        doId: this.doId,
        doName: this.doName,
        accName1: this.accName1,
        accAmount1: this.accAmount1,
        totalAmount: this.yinsAmount,
        amount: this.yinsAmount,
        remark: this.remark
      };
      tool.post('bill/saveScanReceipt', model).then((ret) => {
        if (ret.stat) {
          this.clearData()
        }
      });
    },
    clearData () {
      this.billDate = tool.getDay()
      this.accId = null
      this.accId1 = null
      this.accName = null
      this.accName1 = null
      this.accAmount = null
      this.accAmount1 = null
      this.remark = null
      this.$emit('clearData')
    }
  }
};
</script>
<style scoped lang="scss">
.sk {
  padding: 14px;
  border: 1px solid rgba(0,0,0,0.2);
  margin-right: 5px;
  height: 352px;
}
.receipt-info {
  display: flex;
  .labelh {
    font-size: 13px;
    color: #515a6e;
    width: 60px;
    padding: 5px 0;
  }
  .con {
    padding: 3px 0;
  }
}
</style>